package cn.tedu.orderservice.rabbitmq;

import cn.tedu.orderservice.config.RabbitMqConfig;
import cn.tedu.orderservice.repository.OrderRepository;
import com.rabbitmq.client.Channel;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class RabbitMQConsume {

    @Autowired
    private OrderRepository orderRepository;

    @RabbitListener(queues = RabbitMqConfig.ORDER_DEAD_LETTER_QUEUE)
    public void orderDeadLetter(String orderNo, Message message, Channel channel) throws Exception {
        log.info("死信队列消费消息:{}",orderNo);
        orderRepository.updateStatusFromNormalToException(orderNo);

        //手动ack,amqp协议设置自动ack,所以无需手动ack,也不会报错
//        long deliveryTag = message.getMessageProperties().getDeliveryTag();
//        channel.basicAck(deliveryTag,true);
    }
}
